package manager;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.DBManager;

public class UpdateServlet extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {

			request.setCharacterEncoding("UTF-8");

			String name= request.getParameter("name");
			String ruby= request.getParameter("ruby");
			String password = request.getParameter("password");
			String id = request.getParameter("id");
			String resultMessage=null;

			String update_sql = "update managers set name=?, ruby=?, password=? where id=?";
			Connection con = null;
			Statement smt = null;

			try {
				con = DBManager.getConnection();
				smt = con.createStatement();

				PreparedStatement ps = con.prepareStatement(update_sql);
				ps.setString(1, name);
				ps.setString(2, ruby);
				ps.setString(3, password);
				ps.setInt(4, Integer.parseInt(id));
				ps.executeUpdate();
				resultMessage="変更しました";
				}
				catch (SQLException e){
					throw new ServletException(e);
				}
				finally {
					if (smt != null) {
						try{
							smt.close();
						}
						catch (SQLException ignore) {
							//		何もしない
						}
						if (con != null) {
							try {
								con.close();
							}
							catch (SQLException ignore) {
								//		何もしない
							}
						}
					}
				}
				request.setAttribute("msg", resultMessage);
				request.getRequestDispatcher("/manager/ListServlet").forward(request, response);

			}
	}